/**
 * 
 */
package org.vsg.common.pagination.processor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Vector;

import org.vsg.common.pagination.datatrans.DataTransformer;
import org.vsg.common.pagination.keyword.PaginationKeyword;

/**
 * @author Bill vison
 *
 */
public class CollectionPaginationProcessor implements PaginationProcessor {
	
	
	/* (non-Javadoc)
	 * @see org.vsg.commons.util.pagination.processor.PaginationProcessor#getTotalRecords(org.vsg.commons.util.pagination.PaginationRequestDataTransformer)
	 */
	public int getTotalRecords(DataTransformer transformer)
			throws PaginationProcessorException {
		// TODO Auto-generated method stub
		int count = 0;
		
		
		Object obj = transformer.getValue(PaginationKeyword.TFS_ARRAYALLLIST);
		Collection allDataList = (Collection)obj;
		count = allDataList.size();

		return count;
	}

	/* (non-Javadoc)
	 * @see org.vsg.commons.util.pagination.processor.PaginationProcessor#getPaginationList(org.vsg.commons.util.pagination.PaginationRequestDataTransformer)
	 */
	public List getPaginationList(DataTransformer transformer)
			throws PaginationProcessorException {
		// TODO Auto-generated method stub

		List list = Collections.synchronizedList(new ArrayList());

		int offset = Integer.parseInt(transformer.getImplicitValue(PaginationKeyword.TFS_DATA_OFFSET).toString());
		int pageSize = Integer.parseInt(transformer.getImplicitValue(PaginationKeyword.TFS_ROWS).toString());
		
		Collection coll = (Collection)transformer.getValue(PaginationKeyword.TFS_ARRAYALLLIST);
		Vector collection = new Vector(coll);
		int maxPosi = (offset + pageSize) > collection.size() ? collection.size() : (offset + pageSize);

		for (int i = offset ; i < maxPosi ;i++) {
			list.add( collection.get(i));
		}
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.vsg.commons.util.pagination.processor.PaginationProcessor#initProcessor()
	 */
	public void initProcessor() throws PaginationProcessorException {
		// TODO Auto-generated method stub
		
	}

	
	
}
